var tmpObj;

$(document).ready(function() {	
	findAllDeptType();
	findAllRole();
	
	$('#selectUser').click(function() {
		//alert("ggggggggggggggggggggggggggggggggggggggggggggg");
		tmpObj=$(this);	
		$("#leaderChooseWindow").dialog("open");
		//$("#deptChooseWindow1").dialog("open");
	});

	$("#leaderChooseWindow").dialog({
		autoOpen : false,
		minWidth : 520,
		modal : true,
		title : '选择人员',
		close : function() {
			return false;
		}
	});

	//取消通知带班领导
	$("#cancelNotify").click(function(){
		$("#deptChooseWindow").dialog('close');
		window.location.href="/ag/work/event_query_start.jsp";
	});
	//确定选择人员
	$("#selOk").click(function(){
		tmpObj.prev().prev().text($("#selDepts option").get(0).text);
		tmpObj.prev().text($("#selDepts option").get(0).value);
		tmpObj.val("修改");
		$("#leaderChooseWindow").dialog('close');
	});
	//取消选择人员
	$("#selCancel").click(function(){
		$("#leaderChooseWindow").dialog('close');
	});
	// 测试弹出通知带班领导的选择窗体
//	$('#maps').click(function() {
//		$("#notifyLeaderWindow").dialog("open");
//	});
/*
	$("#selectUser").click(function() {
		 $("#leaderChooseWindow").dialog("open");
	});*/
	$("#dept").click(function(){
//		$("#deptArea").show();
		$("#roleArea").hide();
		$("#subDepts").empty();
		$("#subDeptstd").hide();
		$("#deptultd").show();
	});
	$("#role").click(function(){
//		$("#deptArea").hide();
		$("#roleArea").show();
		$("#subDeptstd").show();
		$("#subDepts").empty();
		$("#deptultd").hide();
	});
	// 指派部门的窗体上两个按钮的响应函数 addDept 和 delDept
	$("#addDept").click(function() {
		if ($("#subDepts option:selected").length > 0) {
			$("#subDepts option:selected").each(function() {
				var strHtml="";
				var udId = $(this).val();
				var udName = $(this).text();
				if($("#selDepts option").length == 1){
					alert("只能选择一个人！");
					return ;
				}
				if ($("#selDepts option").length > 0 ) { //
					var t = true;
					$("#selDepts option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					})
					if(t){
						strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
							+ udName + "</option>";
						//$("#leader").empty().append(udName).css("font-weight","bold");
						//$("#leaderId").empty().append(udId);
					}
				}else{
					strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
						+ udName + "</option>";
					//$("#leader").empty().append(udName).css("color","#960").css("font-weight","bold");
					//$("#leaderId").empty().append(udId);
				}
				$("#selDepts").append(strHtml);
			})
		}
	});

	$("#delDept").click(function() {
		if ($("#selDepts option:selected").length > 0) {
			$("#selDepts option:selected").each(function() {
		//		$("#leader").empty().append("没有选择带班领导").css("color","red").css("font-weight","bold");
		//		$("#leaderId").empty();
				$(this).remove();
			})
		}
	});
	
	
	
	$("#deptType").change(function() {
		var dtId = $("#deptType option:selected").val();
		alert("sdfsdfsdf");
		alert(dtId);
		$.ajax({
  			type: "post",
  			data:{"dtId":dtId},
 			url: "/ag/admin/userDept_findUserDeptByDtId.action",
  			success:function(data){
  				$("#q_UDept").empty();
  				$("#q_UDept").append("<option value='0'>请选择</option>");
  				$.each(data.userDepts,function(i,n){
  					$("#q_UDept").append("<option value="+n.udId+">"+n.udName+"</option>");
				});
  			}
  		});
	});
	$("#q_UDept").change(function() {
		var UDept = $("#q_UDept option:selected").val();
		$.ajax({
			type : "post",
			url : "/ag/admin/queryUser_query.action",
			data : {"queryOption['UDept']":UDept},
			success : function(data){
				$("#subDepts").empty();
				$.each(data.pageUtil.list,function(i,n){
					$("#subDepts").append("<option value="+n.UId+">"+n.UName+"</option>");
				});
			}
		});
	});
	$("#roleList").change(function() {
		var q_URole = $("#roleList option:selected").val();
		$.ajax({
			type : "post",
			url : "/ag/admin/queryUser_query.action",
			data : {"queryOption['URole']":q_URole},
			success : function(data){
				$("#subDepts").empty();
				$.each(data.pageUtil.list,function(i,n){
					$("#subDepts").append("<option value="+n.UId+">"+n.UName+"</option>");
				});
			}
		});
	});
});







//获取所有部门类型
function findAllDeptType(){
	var extern = 0; //为了在循环注册ID时，使各个DOM（span,ul） 'S ID不一样。 
	$.ajax({
			type: "post",
			url: "/ag/admin/userDept_findAllDeptType.action",
			success:function(data){
				$("#deptType").empty();
				$("<option value='0'>请选择</option>").appendTo("#deptType");
				$("#q_UDept").append("<option value='0'>请选择</option>");
				$("#deptul").empty();
				$.each(data.deptTypes,function(i,n){
				$("<option value="+n.dtId+">"+n.dtName+"</option>").appendTo("#deptType");
					//below motified by czy 
				    extern++; 
					var temp = "span_id"+extern;
					var temp2 = "ul_id"+extern;
					$("#deptul").append("<li>"+"<span style=\"cursor: pointer;\" id="+temp+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+n.dtName+"</span>"+"<ul style=\"list-style:none\" id="+temp2+">"+"</ul>"+"</li>");
					var span_id = "#" + $.trim(temp);
					var ul_id = "#" + $.trim(temp2);
					$(span_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
					$(span_id).click(function() {
						//每点击一次改变一次图标
						var imgsrc= $(this).children("img").attr("src");
						if(imgsrc=="/ag/wzp/images/jiahao.gif") $(this).children("img").attr("src","/ag/wzp/images/jianhao.gif");
						else
						$(this).children("img").attr("src","/ag/wzp/images/jiahao.gif");
						
						var dtId = n.dtId;
						$.ajax({
				  			type: "post",
				  			data:{"dtId":dtId},
				 			url: "/ag/admin/userDept_findUserDeptByDtId.action",
				  			success:function(data){
				  			if($(ul_id).text()!=""){$(ul_id).empty();}

				  			else	{
				  			
				  				$.each(data.userDepts,function(i,n){
//			  					$(ul_id).append("<li value="+n.udId+">"+n.udName+"</li>"); 表明传进来的数据中有n.udId n.udName 
				  					extern++;
				  				    var temp3 = "span2_id"+extern;
		  				     	var temp4 = "ul2_id"+extern;
			  					$(ul_id).append("<li>"+"<span style=\"cursor: pointer;\" id="+temp3+">"+"<img src=\"/ag/wzp/images/jiahao.gif\">"+n.udName+"</span>"+"<ul style=\"list-style:none\" id="+temp4+">"+"</ul>"+"</li>");
			  					var span2_id = "#" + $.trim(temp3);
			  					var ul2_id = "#" + $.trim(temp4);
			  					$(span2_id).children("img").attr("src","/ag/wzp/images/jiahao.gif");
			  					$(span2_id).click(function() {
									//每点击一次改变一次图标
									var imgsrc= $(this).children("img").attr("src");
									if(imgsrc=="/ag/wzp/images/jiahao.gif") $(this).children("img").attr("src","/ag/wzp/images/jianhao.gif");
									else
									$(this).children("img").attr("src","/ag/wzp/images/jiahao.gif");
			  					var UDept = n.udId;
			  					$.ajax({
			  						type : "post",
			  						url : "/ag/admin/queryUser_query.action",
			  						data : {"queryOption['UDept']":UDept},
			  						success : function(data){

//			  							if(data.pageUtil.list==null) alert("该部门中没有任何人员。");

			  							if($(ul2_id).text()!=""){$(ul2_id).empty();}
			  							else{
			  							$.each(data.pageUtil.list,function(i,n){
//			  								$("#subDepts").append("<option value="+n.UId+">"+n.UName+"</option>"); 表明传进来的数据中有n.UId .UName
			  								extern++;
			  							    var temp5 = "span3_id"+extern;
//			  								strHtml+="<option class='class1' name='selDepts' selected value='" + udId + "'>"
//			  								+ udName + "</option>"; 模仿的语句
			  								$(ul2_id).append("<li>"+"<span style=\"cursor: pointer;\" id="+temp5+">"+"<img src=\"/ag/wzp/images/people.gif\">"+n.UName+"</span>"+"</li>");
			  								var span3_id = "#" + $.trim(temp5);
			  								$(span3_id).click(function() {
			  									
			  									$("#selDepts").empty();
			  									setTimeout(function(){
				  									$("#selDepts").append("<option class='class1' name='selDepts' selected value='" + n.UId + "'>"
							  								+ n.UName + "</option>");},500);
			  									
			  								});
			  								
			  							});
			  							}
			  						}
			  					});
			  					
			  					});
			  					
			  					
								});}
				  			}
				  		});
						});
					
					
			});
			}
		});
}
	

	
	

	
	
	//获取所有角色
	function findAllRole(){
		$.ajax({
  			type: "post",
 			url: "/ag/admin/Role_findAllRole.action",//因为com.ag.admin.action.RoleAction的controller配的是RoleAction
  			success:function(data){
  				$("#roleList").empty();
  				$("<option value='0'>请选择</option>").appendTo("#roleList");
  				$.each(data.roles,function(i,n){
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo("#roleList");
				});
  			}
  		});
	}
	
	function showMessage(){
		$("#dialog").dialog("destroy");
		$("#dialog-message").dialog({
			height:150,
			width: 280,
			modal: true,
			resizable: false
//			buttons: {
//				Ok: function() {
//					$(this).dialog('close');
//				}
//			}
		});
	}